package com.gdue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdue.commons.R;
import com.gdue.entity.Employee;
import com.gdue.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee>  login(HttpServletRequest request, @RequestBody Employee employee){

        //1.将页面提交的密码进行md5加密处理
        String password = employee.getPassword();
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Employee> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lambdaQueryWrapper);

        //3.如果没有查询到则返回登录失败结果
        if (emp == null) {
            return R.error("登陆失败...");
        }

        //4.密码对比，如果不一致就返回登陆失败结果
        if (!(password.equals(emp.getPassword()))) {
            return R.error("登陆失败...");
        }
        //5.查看员工状态，如果已禁用，就返回账号已禁用结果
        if ("0".equals(emp.getStatus())){
            return R.error("账号已禁用...");
        }
        //6.登录成功，将用户id存入session中并返回登录成功结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest  request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功...");
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest  request,  @RequestBody Employee  employee){
        //添加初始密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //添加创建者，创建时间，修改者，修改时间
//        Long employeeId= (Long)  request.getSession().getAttribute("employee");
//        employee.setCreateUser(employeeId);             //创建者
//        employee.setCreateTime(LocalDateTime.now());    //创建时间
//        employee.setUpdateUser(employeeId);             //修改者
//        employee.setUpdateTime(LocalDateTime.now());    //修改时间
        //调用Service层方法保存创建的Employee
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page>  pageR(int page,int pageSize,String  name){
        //构造分页构造器
        Page    pageInfo=new Page<>(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee>    lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //添加过滤条件        判断名字是否非空
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getUsername,name);
        //添加排序条件
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行sql语句
        employeeService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest  request,@RequestBody Employee employee){
        //添加修改时间
//        employee.setUpdateTime(LocalDateTime.now());
//        //添加修改者
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(empId);
        //执行sql语句
        employeeService.updateById(employee);

        return  R.success("员工信息修改成功");
    }

    /**
     * 修改员工信息的查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee>  getById(@PathVariable String  id){
        Employee emp = employeeService.getById(id);
        if (emp != null) {
            return R.success(emp);
        }
        return R.error("系统繁忙，请稍后重试...");
    }
}
